//
//  SSWelcomeViewController.swift
//  SSSina
//
//  Created by ss on 2017/7/2.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit
import SDWebImage

class SSWelcomeViewController: UIViewController {

    override func loadView() {
        view = bgImageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        setupHeadImageViewAndMessageLabelAnim()
    }
    
    //设置头像和文字动画
    private func setupHeadImageViewAndMessageLabelAnim(){
        
        self.headImageView.snp.updateConstraints({ (make) in
            make.top.equalTo(self.view).offset(100)
        })
        
        //阻尼动画
        //动画时间 , 延迟  阻尼系数, 初始速度
        UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: {
            //强制刷新当前界面
            self.view.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: 0.25, animations: { 
                self.messageLabel.alpha = 1
            },completion:{ (_) in
                //发送通知 切换根控制器到ssMainVC
                NotificationCenter.default.post(name: SSSWITCHROOTVIEWCONTROLLER, object: "welcomeVC")
                
                }
            )
            
        }
    }
    
    //设置视图
    private func setupUI(){
        //添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        //添加约束
        headImageView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp.bottom).offset(20)
        }
        
    }
    //懒加载控件
    //背景图片
    private lazy var bgImageView:UIImageView = UIImageView(named: "ad_background")
    //个人头像
    private lazy var headImageView: UIImageView = {
        let img = UIImageView(named: "avatar_default_big")
        img.layer.cornerRadius = 45
        img.clipsToBounds = true
        img.layer.borderColor = SSThemeColor.cgColor
        img.layer.borderWidth = 1
        
        //使用sdwebImage框架
        img.ss_setImage(withUrlSting: SSOAuthViewModel.share.userAccountModel?.avatar_large)
        return img
    }()
    //欢迎回来
    private lazy var messageLabel: UILabel = {
       let lab = UILabel(text: "欢迎回来", textColor: UIColor.darkGray, fontSize: SSBigFontSize)
        //对齐方式 中心对其
        lab.textAlignment = .center
        lab.alpha = 0
        return lab
    }()
}
